//
//  SendDetailViewController.m
//  DriverUse
//
//  Created by angra on 16/11/2.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "SendDetailViewController.h"
#import "SendDetailTableViewCell.h"
#import "SendDetailHeader.h"
#import "LLPersonMianViewController.h"
#import "HomeDetailModel.h"
@interface SendDetailViewController ()<UITableViewDataSource,UITableViewDelegate,SendDetailDelegate,UIAlertViewDelegate>
@property (nonatomic,retain)UITableView *tabelView;
@property (nonatomic,weak)UIView *statueView;
@property (nonatomic,retain)HomeDetailModel *model;
@property (nonatomic,copy)NSString *phoneNumber;
@end

static NSString *const kSendDetailCellIdentifier = @"SendDetailTableViewCell";
@implementation SendDetailViewController

- (UITableView *)tabelView
{
    if(!_tabelView)
    {
        _tabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, kScaleNum(250/2), kScreenFrameW, kScreenFrameH-64-kScaleNum(250/2)) style:UITableViewStylePlain];
        _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabelView.backgroundColor = kColoeString(@"f7f7f7");
        _tabelView.delegate = self;
        _tabelView.dataSource = self;
        [_tabelView registerClass:[SendDetailTableViewCell class] forCellReuseIdentifier:kSendDetailCellIdentifier];
    }
    return _tabelView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"派单详情";
    self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc] initWithCustomView:[self barButtonItemWithImage:[UIImage imageNamed:@"usertwo"] target:self action:@selector(jumpToPersonVC)]];
    [self setTopView];
    [self.view addSubview:self.tabelView];
    
    [self getData];
    
    
    
}
-(UIView*)barButtonItemWithImage:(UIImage *)image target:(id)target action:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
    btn.size = CGSizeMake(16, 20);
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIView *containView = [[UIView alloc] initWithFrame:btn.bounds];
    [containView addSubview:btn];
    return containView;
}
-(void)getData
{
    
    SendDetailHeader *header = [[SendDetailHeader alloc]init];
    header.frame = CGRectMake(0, 0, kScreenFrameW, kScaleNum(250/2));
    [self.view addSubview:header];
    [AllProjectRequest queryOrderDetail:self.orderId success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"resultObject"];
        
        
        self.model = [[HomeDetailModel alloc] initWithDictionary:dic error:nil];
        NSLog(@"%@",self.model);
        header.model = self.model;
        header.start = self.start;
        header.end = self.end;
        [self.tabelView reloadData];
        
    } failure:^(NSError *error) {
        
    }];


}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, -20, kScreenFrameW, 20)];
    view.backgroundColor = [UIColor colorWithHexString:@"ff9a02"];
    self.statueView = view;
    [self.navigationController.navigationBar addSubview:view];
    NSMutableDictionary * color = [NSMutableDictionary dictionary];
    color[NSForegroundColorAttributeName] = kWhiteColor;
    self.navigationController.navigationBar.titleTextAttributes = color;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"ffac2e"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.statueView removeFromSuperview];
    NSMutableDictionary * color = [NSMutableDictionary dictionary];
    color[NSForegroundColorAttributeName] = kBlackColor;
    self.navigationController.navigationBar.titleTextAttributes = color;
    self.navigationController.navigationBar.barTintColor = RGBColor1(244, 245, 246);
}
-(void)setTopView
{

}
-(void)jumpToPersonVC
{
    LLPersonMianViewController  * personMianViewController = [LLPersonMianViewController new];
    [self.navigationController pushViewController:personMianViewController animated:YES];
}
#pragma mark tableView delegate dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.content.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return kScaleNum(260/2);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SendDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSendDetailCellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    cell.fatherVC = self;
    cell.model = self.model.content[indexPath.row];
    return cell;
}
#pragma mark sendDetailDelegate
-(void)clickPhoneWith:(NSString *)phoneNumber
{
    self.phoneNumber = phoneNumber;
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"拨打电话：%@",phoneNumber] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",self.phoneNumber]]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
